/**
 * Copyright (C) 2011 GreenSoft S.A.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *        http://www.apache.org/licenses/LICENSE-2.0
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package ar.com.greensoft.jeecommons.web.jsf.util;

import javax.faces.application.FacesMessage;
import javax.faces.application.FacesMessage.Severity;
import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;

import org.apache.commons.lang3.StringUtils;

import ar.com.greensoft.neosystem.crm.web.beans.NavigationConstants;

/**
 * @author Jorge E. Villaverde
 * @version 1.0.0
 * @since 06/01/2012	
 */
public abstract class JSFMenuUtil implements NavigationConstants{
	public static final String JSF_REDIRECT = "?faces-redirect=true&amp;includeViewParams=true";
	public static final String JSF_REDIRECT_ESCAPED = "?faces-redirect=true&includeViewParams=true";

	public static String buildMenuPath(String role, String menu, String action){
		StringBuffer builder = new StringBuffer();
		
		builder.append(PATH_SEPARATOR);
		builder.append(PROTECTED_PATH);
		builder.append(PATH_SEPARATOR);
		if(!StringUtils.isEmpty(role)){
			builder.append(role);
			builder.append(PATH_SEPARATOR);
		}
		if(!StringUtils.isEmpty(menu)){
			builder.append(menu);
			builder.append(PATH_SEPARATOR);
		}
		if(!StringUtils.isEmpty(action)){
			builder.append(action);
			builder.append(FILE_EXT);
		}
		builder.append(JSF_REDIRECT_ESCAPED);
		
		return builder.toString();
	}
	
	public static String buildPath(String role, String action){
		StringBuffer builder = new StringBuffer();
		
		builder.append(PATH_SEPARATOR);
		builder.append(PROTECTED_PATH);
		builder.append(PATH_SEPARATOR);
		builder.append(role);
		builder.append(PATH_SEPARATOR);
		builder.append(action);
		builder.append(FILE_EXT);
		builder.append(JSF_REDIRECT_ESCAPED);
		
		return builder.toString();		
	}
	
	public static String buildDefaultMenuPath(String role, String menu){
		return buildMenuPath(role, menu, INDEX_ACTION);
	}
	
	public static void addErrorMessage(String localizedMessage) {
		addMessage(localizedMessage, FacesMessage.SEVERITY_ERROR);
	}	

	public static void addInfoMessage(String localizedMessage) {
		addMessage(localizedMessage, FacesMessage.SEVERITY_INFO);
	}
	
	public static void addMessage(String localizedMessage, Severity severity) {
		addMessage(null, localizedMessage, severity);
	}
	
	public static void addMessage(String clientId, String localizedMessage, Severity severity) {
		FacesContext context = FacesContext.getCurrentInstance();
		UIComponent componet = null;
		if(clientId != null)
			componet = context.getViewRoot().findComponent(clientId);
		
		FacesMessage message = new FacesMessage();
		message.setSeverity(severity);
		message.setSummary(localizedMessage);
		message.setDetail(localizedMessage);
		if(componet == null)
			context.addMessage(null, message);
		else
			context.addMessage(componet.getClientId(), message);
	}
	
}
